# -*- coding: utf-8 -*-

from datetime import datetime
import redis

def seconds_until_midnight():
    """Get the number of seconds until midnight."""
    n = datetime.now()
    return ((24 - n.hour - 1) * 60 * 60) + ((60 - n.minute - 1) * 60) + (60 - n.second)

class db_proxy:
    def __init__(self, host="localhost", port=6379):
        self._host = host
        self._port = port
        self._conn = redis.Redis(host, port)
        if not self._conn.hexists("user:id", "next_seq"):
            self._conn.hset("user:id", "next_seq", 1000)

    def user_exists(self, wx_id):
        return self._conn.hexists("user:id", wx_id)

    def get_user_seq(self, wx_id):
        return self._conn.hget("user:id", wx_id)

    def get_user_wx_id(self, seq):
        return self._conn.hget("user:id", seq)

    def add_user(self, wx_id):
        self._conn.hincrby("user:id", "next_seq")
        seq = self._conn.hget("user:id", "next_seq")
        self._conn.hset("user:id", wx_id, seq)
        self._conn.hset("user:id", seq, wx_id)

        user = {"wx_id":wx_id, "seq":seq}
        return user

    def add_user_pkt_history(self, who, pkt_id):
        key = "user:" + str(who) + ":msg_history"
        self._conn.rpush(key, pkt_id)

    def get_user_pkt_history(self, who, range_begin, range_end):
        key = "user:" + str(who) + ":msg_history"
        return self._conn.lrange(key, range_begin, range_end)

    def add_debt(self, who, count = 1):
        key = "user:" + str(who) + ":debt"
        return self._conn.incrby(key, count)

    def sub_debt(self, who, count = 1):
        key = "user:" + str(who) + ":debt"
        return self._conn.decr(key, count)

    def pkt_exists(self, pkt_id):
        key = "pkt:" + pkt_id
        return self._conn.exists(key)

    def add_new_pkt(self, msg_id, msg_type, msg_from,
        msg_time, msg_url, msg_desc):
        key = "pkt:" + msg_id
        self._conn.hset(key, "type", msg_type)
        self._conn.hset(key, "from", msg_from)
        self._conn.hset(key, "time", msg_time)
        self._conn.hset(key, "url", msg_url)
        self._conn.hset(key, "desc", msg_desc)

    def get_pkt(self, pkt_id):
        key = "pkt:" + pkt_id
        if not self._conn.exists(key):
            return None
        msg = {}
        msg["type"] = self._conn.hget(key, "type")
        msg["from"] = self._conn.hget(key, "from")
        msg["time"] = self._conn.hget(key, "time")
        msg["url"] = self._conn.hget(key, "url")
        msg["desc"] = self._conn.hget(key, "desc")
        return msg

    def pkt_report_count(self, pkt_id):
        key = "report:" + pkt_id
        return self._conn.scard(key)

    def update_pkt_report(self, pkt_id, who):
        key = "report:" + pkt_id
        self._conn.sadd(key, who)
        return self._conn.scard(key)

    def take_from_one_today(self, who, offer):
        key = "taken:" + who + "-from-" + offer
        self._conn.set(key, 1, ex=seconds_until_midnight())

    def has_taken_today(self, who, offer):
        key = "taken:" + who + "-from-" + offer
        return self._conn.exists(key)

    def pkt_shared_count(self, pkt_id):
        key = "shared:" + pkt_id
        return self._conn.scard(key)

    def update_pkt_shared(self, pkt_id, msg_from, who):
        key = "shared:" + pkt_id
        self._conn.sadd(key, who)
        return self._conn.scard(key)

    def has_shared(self, pkt_id, who):
        key = "shared:" + pkt_id
        return self._conn.sismember(key, who)

    def pop_front_from_pkt_pool(self):
        if not self._conn.exists("pkt_pool"):
            return None
        if self._conn.llen("pkt_pool") == 0:
            return None
        return self._conn.lpop("pkt_pool")

    def push_back_to_pkt_pool(self, pkt_id):
        self._conn.rpush("pkt_pool", pkt_id)

    def push_front_to_pkt_pool(self, pkt_id):
        self._conn.lpush("pkt_pool", pkt_id)

if __name__ == "__main__":
    db = db_proxy()
    left = db.add_debt(1001, 5)
    print left
    left = db.sub_debt(1001)
    print left
